package cn.xianshu.pet.controller;


import cn.xianshu.basic.exception.BasicException;
import cn.xianshu.basic.util.AxiosResult;
import cn.xianshu.basic.util.LoginContextUtil;
import cn.xianshu.basic.util.PageList;
import cn.xianshu.pet.domain.Pet;
import cn.xianshu.pet.domain.SearchMasterMsg;
import cn.xianshu.pet.query.SearchMasterMsgQuery;
import cn.xianshu.pet.service.ISearchMasterMsgService;
import cn.xianshu.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;



@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService searchMasterMsgService;

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return searchMasterMsgService.findAll();
    }

    /**
     * 发布寻主
     * @param smm
     * @param request
     * @return
     */
    @PostMapping("/publish")
    public AxiosResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            searchMasterMsgService.publish(smm, logininfo.getId());
            return AxiosResult.me();
        } catch (BasicException e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常");
        }
    }

    /**
     * 待处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody  SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return searchMasterMsgService.pending(query, logininfo.getId());
        } catch (BasicException e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 已处理
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            return searchMasterMsgService.processed(query, logininfo.getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 待处理数据
     * @param pet
     * @param request
     * @return
     */
    @PutMapping("/handle")
    public AxiosResult handle(@RequestBody Pet pet, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            searchMasterMsgService.handle(pet, logininfo.getId());
            return AxiosResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AxiosResult.me().setMessage("系统异常!");
        }
    }

}
